package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsSystem;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;

import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.framework.upms.biz.system.mapper.UpmsSystemMapper;
import cn.chiship.framework.upms.biz.system.entity.UpmsSystemExample;
import cn.chiship.framework.upms.biz.system.service.UpmsSystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统业务接口实现层
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsSystemServiceImpl extends BaseServiceImpl<UpmsSystem, UpmsSystemExample> implements UpmsSystemService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsSystemServiceImpl.class);

    @Resource
    UpmsSystemMapper upmsSystemMapper;

    @Override
    public BaseResult insertSelective(UpmsSystem upmsSystem) {
        upmsSystem.setIsDisabled(BaseConstants.NO);

        UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
        upmsSystemExample.createCriteria().andCodeEqualTo(upmsSystem.getCode());
        List<UpmsSystem> upmsSystems = upmsSystemMapper.selectByExample(upmsSystemExample);

        if (upmsSystems.isEmpty()) {
            return super.insertSelective(upmsSystem);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "系统编码已存在,请重新输入");
        }

    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsSystem upmsSystem) {
        UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
        upmsSystemExample.createCriteria().andCodeEqualTo(upmsSystem.getCode()).andIdNotEqualTo(upmsSystem.getId());
        List<UpmsSystem> upmsSystems = upmsSystemMapper.selectByExample(upmsSystemExample);
        if (upmsSystems.isEmpty()) {
            return super.updateByPrimaryKeySelective(upmsSystem);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "系统编码已存在,请重新输入");
        }
    }

    @Override
    public BaseResult validateExistByField(String id, String field, String value) {
        boolean flag = true;
        UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
        UpmsSystemExample.Criteria criteria = upmsSystemExample.createCriteria();
        if (!StringUtil.isNullOrEmpty(id)) {
            criteria.andIdNotEqualTo(id);
        }
        if ("title".equals(field)) {
            criteria.andTitleEqualTo(value);
        } else {
            flag = false;
        }
        if (flag) {
            return BaseResult.ok(upmsSystemMapper.countByExample(upmsSystemExample) > 0);
        } else {
            return BaseResult.error(BaseResultEnum.FAILED, "暂未配置对字段" + field + "的校验");
        }
    }

}
